package at.sysarch.filter;

import java.util.LinkedList;

import at.pimpmypipe.filter.DataEnrichmentFilter;
import at.sysarch.model.Line;

public class LineToListFilter extends
		DataEnrichmentFilter<Line, LinkedList<Line>> {

	public LineToListFilter() {
	}

	/**
	 * Takes a line and splits the data String around a regular expression.
	 * Returns a List containing each resulting word with the line index.
	 */
	@Override
	protected boolean fillEntity(Line nextVal, LinkedList<Line> entity) {
			
		if (nextVal.getData().length() == 0) {
			return false;
		}
		
		// Split around [one or more whitespace character] or [at least two non-word characters] or [one of " & ( )]
		String[] wordsOfLine = nextVal.getData().split("[\\s]+|[\\W]{2,}|[\"&()]");
		if (wordsOfLine != null) {
			
			for (String s : wordsOfLine) {
				if (s.length() > 0) {
					Line line = new Line();
					line.setData(s);
					line.setIndex(nextVal.getIndex());
					entity.add(line);
				}
			}
		}
		
		return true;
	}

	@Override
	protected LinkedList<Line> getNewEntityObject() {
		return new LinkedList<Line>();
	}

}
